package com.idp.web.test.controller;

import com.idp.web.test.entity.Stock;
import com.idp.web.test.entity.Warehouse;
import com.idp.web.test.service.StockService;
import com.idp.web.test.service.WarehouseService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.common.base.BaseController;
import com.idp.common.persistence.Page;
import com.idp.common.util.ValidateUtils;

import net.sf.json.JSONObject;

/**
 * 
 * 123123controller
 * 
 * <pre>
 * 	历史记录：
 * 	2017-06-02 Amber
 * 	新建文件
 * </pre>
 * 
 * @author
 * 
 * 		<pre>
 * SD
 * 	Amber
 * PG
 *	Amber
 * UT
 *
 * MA
 *         </pre>
 * 
 * @version $Rev$
 *
 *          <p/>
 *          $Id$
 *
 */
@Controller
@RequestMapping("/stock")
public class StockController extends BaseController {

	private Logger logger = Logger.getLogger(StockController.class);

	@Resource
	private StockService stockService;

	@Resource
	private WarehouseService warehouseService;

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	初始化查询页面
	 * </pre>
	 * 
	 * @return
	 */
	@RequestMapping("/init")
	public String init(HttpServletRequest request) {
		try {
			request.setAttribute("warehouses", warehouseService.findBySearch(new Warehouse()));
		} catch (Exception e) {
			// TODO: handle exception
		}
		return "shopping/stock/stockSearch";
	}

	@RequestMapping("/searchList")
	public String searchList(Stock stock, Page<Stock> page, HttpServletRequest request) {
		try {
			if (stock.getWarehouseId() == 0) {
				stock.setWarehouseId(null);
			}
			request.setAttribute("page", stockService.sreachByWarehouseAndProductName(stock, page));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "shopping/stock/stockList";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	新增修改页面初始化
	 * </pre>
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/stock")
	public String stock(Integer id, HttpServletRequest request) {

		try {

			if (ValidateUtils.isNotEmpty(id)) {
				Stock stock = stockService.getById(id);
				request.setAttribute("stock", stock);
			}
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
		}

		return "shopping/stock/stock";
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	保存
	 * </pre>
	 * 
	 * @param stock
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(Stock stock) {

		JSONObject json = new JSONObject();

		try {

			// 修改
			if (ValidateUtils.isNotEmpty(stock.getId())) {

				stockService.update(stock);
			}
			// 新增
			else {

				stockService.add(stock);
			}
			json.put("result", "save_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "save_fail");
		}

		return json.toString();
	}

	/**
	 * 
	 * <pre>
	 * 	2017-06-02 Amber
	 * 	删除
	 * </pre>
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete")
	@ResponseBody
	public String delete(Integer id) {

		JSONObject json = new JSONObject();

		try {

			stockService.delete(id);
			json.put("result", "delete_success");
		} catch (Exception e) {

			logger.error(e.getMessage(), e);
			json.put("result", "delete_fail");
		}

		return json.toString();
	}
}
